<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="false">
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="540px" @opened="dialogOpened">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item v-if="false" label="ID">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="项目名称" prop="projectId">
            <el-input v-model="form.projectName" :disabled="true" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="项目阶段" prop="stageId">
            <el-input v-model="form.stageName" :disabled="true" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="累计完成进度">
            <el-input v-model="form.progress" style="width: 370px;" />
          </el-form-item>
          <el-form-item v-if="form.id != null" label="填报时间">
            <el-date-picker v-model="form.updateTime" :disabled="true" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结束时间" prop="startTime">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="此周期项目所耗时（人/天）">
            <el-input v-model="form.timeUse" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="具体描述">
            <el-input v-model="form.describe" :rows="5" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        :row-class-name="rowClassSequenceNo"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sequence')" prop="sequence" label="序号" width="60" />
        <el-table-column v-if="columns.visible('projectId')" prop="projectId" label="项目名称">
          {{ formDefaultValue.projectName }}
        </el-table-column>
        <el-table-column v-if="columns.visible('stageId')" prop="stageId" label="阶段名称">
          {{ formDefaultValue.stageName }}
        </el-table-column>
        <el-table-column v-if="columns.visible('describe')" prop="describe" label="具体描述" width="300px" />
        <el-table-column v-if="columns.visible('progress')" prop="progress" label="任务累计完成进度" />
        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="填报时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('timeUse')" prop="timeUse" label="此周期项目所耗时（人/天）" />
        <el-table-column v-permission="['admin','projectStageProgress:edit','projectStageProgress:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProjectStageProgress from '@/api/projectStageProgress'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '项目周报清单', url: 'api/projectStageProgress', sort: 'id,desc', crudMethod: { ...crudProjectStageProgress }})
const defaultForm = {
  id: null,
  projectId: null,
  stageId: null,
  progress: null,
  updateTime: null,
  startTime: null,
  endTime: null, timeUse: null,
  describe: null
}
export default {
  name: 'ProjectStageProgressList',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'projectStageProgress:add'],
        edit: ['admin', 'projectStageProgress:edit'],
        del: ['admin', 'projectStageProgress:del']
      },
      rules: {
        projectId: [
          { required: true, message: '项目不能为空', trigger: 'blur' }
        ],
        stageId: [
          { required: true, message: '项目阶段不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'describe', display_name: '具体描述' },
        { key: 'stageId', display_name: '项目阶段ID' }
      ],
      formDefaultValue: {
        projectId: 0,
        projectName: '',
        stageId: 0,
        stageName: ''
      },
      pageCount: 0
    }
  },
  mounted() {
    this.crud.props.tableColumns.projectId.visible = false
    this.crud.props.tableColumns.stageId.visible = false
  },
  created() {
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      console.log('one')
      this.formDefaultValue.projectId = this.$route.query.projectId
      this.formDefaultValue.projectName = this.$route.query.projectName
      this.formDefaultValue.stageId = this.$route.query.stageId
      this.formDefaultValue.stageName = this.$route.query.stageName
      console.log(this.formDefaultValue)
      this.crud.queryInput.type = 'stageId'
      this.crud.queryInput.value = this.$route.query.stageId
      this.dialogOpened()
    }
    // 注意这里需要手动refreash加载数据，如果crud自动加载数据，则无法保证设置query发生在refreah之前
    this.crud.refresh()
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.queryInput.type = 'stageId'
      this.crud.queryInput.value = this.formDefaultValue.stageId
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      // const query = this.query
      // if (query.type && query.value) {
      //   this.crud.params[query.type] = query.value
      // }
      return true
    },
    rowClassSequenceNo({ row, rowIndex }) {
      // 把每一行的索引放进row.id
      row.sequence = rowIndex + 1
    },
    dialogOpened() {
      this.form.projectName = this.formDefaultValue.projectName
      this.form.projectId = this.formDefaultValue.projectId
      this.form.stageId = this.formDefaultValue.stageId
      this.form.stageName = this.formDefaultValue.stageName
      this.form.updateTime = new Date().getTime()
    }
  }
}
</script>

<style scoped>

</style>
